package com.duomai.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.admin.SQLite.SqLiteOpenHelper;
import com.example.admin.SQLite.User;
import com.example.admin.SQLite.dao.UserDao;
import com.example.admin.base.BaseActivity;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;

public class LoginActivity extends BaseActivity {

    EditText tvName, tvPass;
    CheckBox cb;
    UserDao userDao;
    @Override
    public void setContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login);
        userDao=new UserDao(this);
        tvName = findViewById(R.id.tvName);
        tvPass = findViewById(R.id.tvPass);
        cb = findViewById(R.id.cb);
        if (Hawk.contains("cb")) {
            if (Hawk.get("cb")) {
                tvName.setText(Hawk.get("us"));
                tvPass.setText(Hawk.get("pass"));
                cb.setChecked(true);
            }

        }
        findViewById(R.id.tv_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = tvName.getText().toString().trim();
                String pass = tvPass.getText().toString().trim();
                ArrayList<User> list = userDao.findWhereUser(name);

                if (list.size() == 1) {
                    String pas = list.get(0).UserPass;
                    if (!TextUtils.equals(pas, pass)) {
                        toast("密码错误");
                    } else {
                        Hawk.put("user", list.get(0));
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        if (cb.isChecked()) {
                            Hawk.put("pass", pass);
                            Hawk.put("us", name);
                            Hawk.put("cb", true);
                        } else {
                            Hawk.put("cb", false);
                        }
                    }
                } else {
                    toast("账号不存在");
                }


            }
        });
        findViewById(R.id.tv_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });

    }

    @Override
    public void setInitView() {

    }
}